Component({
  properties: {
    // 接收外部传入的商品数据
    allGoods: {
      type: Array,
      value: [],
      observer: 'filterAndSortGoods'  // 数据变化时重新筛选
    },
    // 下拉选项配置
    option1: {
      type: Array,
      value: [
        { text: '全部商品', value: 0 },
        { text: '新款商品', value: 1 },
        { text: '活动商品', value: 2 },
      ]
    },
    option2: {
      type: Array,
      value: [
        { text: '默认排序', value: 'a' },
        { text: '价格排序', value: 'b' },
        { text: '销量排序', value: 'c' },
      ]
    },
    // 自定义样式
    customClass: {
      type: String,
      value: ''
    },
    // 导航栏高度
    barHeight: {
      type: Number,
      value: 100
    }
  },
  data: {
    value1: 0,
    value2: 'a',
    filteredGoods: [],
    priceSortAsc: true
  },
  methods: {
    onCategoryChange(e) {
      this.setData({ value1: e.detail });
      this.filterAndSortGoods();
    },
    onSortChange(e) {
      if (e.detail === 'b') {
        this.setData({ priceSortAsc: !this.data.priceSortAsc });
      }
      this.setData({ value2: e.detail });
      this.filterAndSortGoods();
    },
    filterAndSortGoods() {
      // 原有筛选逻辑保持不变
      const { value1, value2, allGoods } = this.data;
      let goods = [...allGoods];
      
      if (value1 !== 0) {
        goods = goods.filter(item => item.category === value1);
      }
      
      switch(value2) {
        case 'a':
          goods.sort((a, b) => a.id - b.id);
          break;
        case 'b':
          goods.sort((a, b) => this.data.priceSortAsc ? a.price - b.price : b.price - a.price);
          break;
        case 'c':
          goods.sort((a, b) => b.sales - a.sales);
          break;
      }
      
      this.setData({ filteredGoods: goods });
      // 触发事件，将筛选结果传递给父组件
      this.triggerEvent('filterChange', { filteredGoods: goods });
    }
  },
  lifetimes: {
    attached() {
      if (this.data.allGoods.length > 0) {
        this.filterAndSortGoods();
      }
    }
  }
})